package com.cn.wanxi.util;

import com.cn.wanxi.model.ProductModel;

import java.sql.*;
import java.util.List;

public class JDBC {
    private static Connection getConnection(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/sys?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
            String user = "root";
            String password = "531203";
            return  DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;

    }
//    1.49
public static int excute(String sql) {
    int result = 0;
    try {

        PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
        result = preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return result;
}

    //查询sql方法
    public static ResultSet query(String sql) {
        ResultSet result = null;
        try {
            PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
            result = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}
